import { _decorator, Component, Label, Node } from 'cc';
import { losChannel } from '../../app/Script/SDK/losChannel';
import { StaminaSystem } from '../../app/Script/StaminaSystem/StaminaSystem';
import { losTools } from '../../app/Script/Tools/losTools';
const { ccclass, property } = _decorator;

@ccclass('RewardBoxFrame')
export default class rewardBoxFrame extends Component {
    @property({ type: [Node], tooltip: '全部宝箱节点' })
    allBoxNode = [];
    @property({ type: Node, tooltip: '底部钥匙节点' })
    keyParentNode = null;
    @property({ type: [Node], tooltip: '单个钥匙节点' })
    keyNodeArr = [];
    @property({ type: Node, tooltip: '底部看广告节点' })
    adContainerNode = null;
    @property({ type: Node, tooltip: '不看视频圆圈' })
    watchAdOff = null;
    @property({ type: Node, tooltip: '看视频圆圈' })
    watchAdOn = null;
    @property({ type: Node, tooltip: '获得钥匙按钮' })
    getKeyBtn = null;
    @property({ type: Node, tooltip: '关闭按钮' })
    closeBtn = null;
    @property({ type: Node, tooltip: '关闭节点' })
    close = null;
    //    /**拥有的钥匙数量 */
    hasKeyCnt = 3;
    selectBoxId = 0;
    //    /**全部钥匙数组 */
    allKeyIdx = [0, 1, 2, 3, 4, 5, 6, 7, 8];
    //    /**拥有钥匙数组 */
    hasKeyArr = [];
    openBoxCnt = 0;
    onLoad() {
        console.error("rewardBoxFrame");
        this.adContainerNode.active = false;
        this.keyParentNode.active = true;
        this.watchAdOn.active = true;
        this.close.active = false;


        // losChannel.instance().doAction(1, ["vivo", 'oppo','xiaomi'], (enabled, val) => {
        //     if (enabled) {
        //         losChannel.instance().showCustomAd(false, { top: losChannel.instance().getSysInfo().windowHeight / 2 - 100 })
        //     }
        // })


        this.allKeyIdx = this.allKeyIdx.sort(() => Math.random() - 0.5);
        for (let i = this.allKeyIdx.length - 1; i >= 0; i--) {
            if (this.hasKeyArr.length < losTools.getRandomInt(1, 3)) {
                this.hasKeyArr.push(this.allKeyIdx[i]);
                this.allKeyIdx.splice(i, 1);
            } else {
                break;
            }
        }
        for (let i = 0; i < this.allBoxNode.length; i++) {
            if (this.hasKeyArr.indexOf(i) >= 0) {
                this.allBoxNode[i].getChildByName('video').active = false;
            } else {
                this.allBoxNode[i].getChildByName('video').active = true;
            }
        }
        for (let i = 0; i < this.allBoxNode.length; i++) {
            this.allBoxNode[i].on(Node.EventType.TOUCH_END, () => {
                if (this.hasKeyCnt > 0 && this.allBoxNode[i].getChildByName('rewardBox1').active && this.hasKeyArr.indexOf(i) >= 0) {
                    this.selectBoxId = i + 1;
                    this.keyNodeArr[3 - this.hasKeyCnt].getChildByName('yaoshi').active = false;
                    this.hasKeyCnt--;
                    this.updateBoxUi(i);
                    if (this.hasKeyCnt <= 0) {
                        this.showAdContainer();
                    }
                    console.log("钥匙打开宝箱");
                }
                else {
                    if (this.allBoxNode[i].getChildByName('rewardBox1').active) {
                        let me = this;
                        losChannel.instance().showVideo({
                            sucCall: () => {
                                me.updateBoxUi(i);
                                let idx = me.allKeyIdx.indexOf(i);
                                me.allKeyIdx.splice(idx, 1);
                                console.log("视频打开宝箱: ", this.allKeyIdx, i);
                            },
                            failCall: () => {
                                console.log("视频打开宝箱失败");
                            },
                            endCall: () => {
                                console.log("视频打开宝箱结束");
                            }
                        })

                    }
                }
            });
        }
        this.watchAdOff.on(Node.EventType.TOUCH_END, () => {
            this.watchAdOn.active = !this.watchAdOn.active;
            if (this.watchAdOn.active) {
                this.closeBtn.getComponent(Label).string = '暂时使用';
            } else {
                this.closeBtn.getComponent(Label).string = '暂不使用';
            }
        });
        this.closeBtn.on(Node.EventType.TOUCH_END, () => {
            if (this.watchAdOn.active) {//看广告获取钥匙
                let me = this;
                losChannel.instance().showVideo({
                    sucCall: () => {
                        me.showGetKeyUI();
                        console.error("点击暂时使用获取钥匙");
                    },
                    failCall: () => {
                        console.error("点击暂时使用获取钥匙失败");
                    },
                    endCall: () => {
                        console.error("点击暂时使用获取钥匙结束");
                    }
                })
            } else {
                this.hide();
            }
        });
        this.getKeyBtn.on(Node.EventType.TOUCH_END, () => {
            let me = this;
            losChannel.instance().showVideo({
                sucCall: () => {
                    me.showGetKeyUI();
                    console.error("点击按钮获取钥匙");
                },
                failCall: () => {
                    console.error("点击按钮获取钥匙失败");
                },
                endCall: () => {
                    console.error("点击按钮获取钥匙结束");
                }
            })
        });
        this.close.on(Node.EventType.TOUCH_END, () => {
            this.hide();
        });
        this.closeBtn.active = false;
        if (this.watchAdOn.active) {
            this.closeBtn.getComponent(Label).string = '暂时使用';
        } else {
            this.closeBtn.getComponent(Label).string = '暂不使用';
        }
    }
    //    /**更新宝箱ui */
    updateBoxUi(idx) {
        this.allBoxNode[idx].getChildByName('rewardBox1').active = false;
        /**给玩家添加金币 */
        let coin = this.getReward();
        this.allBoxNode[idx].getChildByName('rewardNum').getComponent(Label).string = coin;
        this.allBoxNode[idx].getChildByName('video').active = false;
        this.openBoxCnt++;
        if (this.openBoxCnt >= 3) {
            this.close.active = true;
        }
    }
    //    /**看广告加一把钥匙 */
    showGetKeyUI() {
        if (this.openBoxCnt >= 9) {
            return;
        }
        if (this.hasKeyCnt >= 3) {
            return;
        }
        this.keyParentNode.active = true;
        this.adContainerNode.active = false;
        if (this.hasKeyCnt == 0) {
            this.keyNodeArr[2].getChildByName('yaoshi').active = true;
            this.hasKeyCnt = 1;
        } else if (this.hasKeyCnt == 1) {
            this.keyNodeArr[1].getChildByName('yaoshi').active = true;
            this.hasKeyCnt = 2;
        } else if (this.hasKeyCnt == 2) {
            this.keyNodeArr[0].getChildByName('yaoshi').active = true;
            this.hasKeyCnt = 3;
        }
        for (let i = this.allKeyIdx.length - 1; i >= 0; i--) {
            this.hasKeyArr.push(this.allKeyIdx[i]);
            this.allKeyIdx.splice(i, 1);
            break;
        }
        for (let i = 0; i < this.allBoxNode.length; i++) {
            if (this.hasKeyArr.indexOf(i) >= 0) {
                this.allBoxNode[i].getChildByName('video').active = false;
            } else {
                if (this.allBoxNode[i].getChildByName('rewardBox1').active) {
                    this.allBoxNode[i].getChildByName('video').active = true;
                }
            }
        }
    }
    hide() {
        this.node.destroy();
        // // 增加体力值的逻辑
        // let power = User.get(UserData.POWER);
        // let t = 1; // 这里的t值需要根据实际情况调整
        // User.set(UserData.POWER, power + this.openBoxCnt * t);
        // Tip.show("体力+" + this.openBoxCnt * t);
        // Event.emit(GameEvent.POWER_EFFECT, true);
        StaminaSystem.instance.addStamina(this.openBoxCnt)

        // //这里的弹窗就是win的初始化的弹窗
        // 
        // window.__prv.doAction(1, ['vivo']);
        // 
        // window.__prv.doAction(1, ['oppo']);

    }
    showAdContainer() {
        this.keyParentNode.active = false;
        this.adContainerNode.active = true;
        this.closeBtn.active = true;
    }
    getReward() {
        return 1;
    }
}